package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Customer;
import com.atguigu.jxc.service.CustomerService;
import org.apache.shiro.crypto.hash.Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("customer")
public class CustomerController {

    @Autowired
    CustomerService customerService;


   // 客户列表分页（名称模糊查询）
/**
 * 请求URL：http://localhost:8080 /customer/list
 * 请求参数：Integer page, Integer rows, String  customerName
 * 请求方式：POST
 * 返回值类型：JSON
 * 返回值：Map<String,Object>
 */
@PostMapping("list")
  public Map<String,Object> list(Integer page, Integer rows, String  customerName){
         ArrayList<Customer> list= customerService.list(page,rows,customerName);
    Map hash=new HashMap<>();
       hash.put("total",list.size());
       hash.put("rows",list);
       return  hash;
  }


//客户添加或修改
    /**
     * 请求URL：http://localhost:8080/ customer/save?customerId=1
     * 请求参数：Customer customer
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */

    @PostMapping("/save")
   public ServiceVO save(Integer customerId, Customer customer){
          if (customerId==null){
              //是保存
               customerService.save(customer);
               return  new ServiceVO(100,"请求成功",null);
          }else{
              //是修改
              customer.setCustomerId(customerId);
              customerService.update(customer);
              return  new ServiceVO(100,"请求成功",null);
          }
   }


    /**
     * 请求URL：http://localhost:8080/customer/delete
     * 请求参数：String  ids
     * 请求方式：POST
     * 返回值类型：JSON
     * 返回值：ServiceVO
     */
    @PostMapping("delete")
    public  ServiceVO delete(Integer ids){
          customerService.delete(ids);
          return  new ServiceVO(100,"请求成功",null);
    }


}
